﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Beway.ValueAdded.Model;
using Beway.ValueAdded.Service;
using beway.common;

namespace Beway.ValueAdded.Admin.Controllers
{
    public class ReUserController : BaseController
    {
        //
        // GET: /ReUser/

        ReUserServ reUserSvc = SingletonProvider<ReUserServ>.Instance;
        AdminUserServ admSvc = SingletonProvider<AdminUserServ>.Instance;
        public ActionResult List()
        {
            return View();
        }

        /// <summary>
        /// 生成随机key
        /// </summary>
        /// <returns></returns>
        public ActionResult GenerateKey()
        {
            string key = Encryption.Md5Encry32bit(StringUtil.GenerateCheckCode(6));
            Dictionary<string, string> dic = new Dictionary<string, string>();
            dic.Add("key", key);
            return Json(dic);
        }

        public JsonResult ReUserList(int? page, int? rows, string UserName)
        {
            int ListCount = 0;
            int adminId = 0;
            if (CurrentRole == ManagerRoleType.MerchantAdmin)
            {
                adminId = CurrentAdmin.AdminID;
            }
            var d = reUserSvc.ReUserList(page ?? 0, rows ?? 0, UserName, adminId, out ListCount);

            Dictionary<string, object> json = new Dictionary<string, object>();
            json.Add("total", ListCount);
            json.Add("rows", d);
            return Json(json, JsonRequestBehavior.AllowGet);
        }

        public JsonResult JsonList()
        {
            int ListCount = 0;
            int adminId = 0;
            if (CurrentRole == ManagerRoleType.MerchantAdmin)
            {
                adminId = CurrentAdmin.AdminID;
            }
            DateTime now = System.DateTime.Now;
            //只显示有效的项目
            var d = reUserSvc.ReUserList(1, 10000, null, adminId, out ListCount)
                .Where(m=>m.RechStatus>0 && m.BeginTime < now && m.EndTime >= now);

            return Json(d.ToList(), JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public ActionResult AddReUser(ReUser Model)
        {
            Model.RechIpWhiteList = Model.RechIpWhiteList.Replace(";", ",");
            string[] ipArray = Model.RechIpWhiteList.Split(',');
            if (ipArray.Length > 10) //最多10个ip
            {
                return Content("0");//失败
            }
            Model.RechStock = 0;
            Model.RechAmount = 0;

            Model.AddTime = System.DateTime.Now;
            int Result = reUserSvc.AddReUser(Model);
            return Content(Result.ToString());
        }

        [HttpPost]
        public ActionResult UpdateReUser(ReUser Model)
        {
            int Result = reUserSvc.UpdateReUser(Model);
            return Content(Result.ToString());
        }
        [HttpPost]
        public ActionResult DelReUser(int id)
        {
            int isdel = reUserSvc.DelReUser(id);
            string ret = "";
            if (isdel > 0)
            {
                ret = "ok";
            }
            else
            {
                ret = "删除失败";
            }
            return Content(ret);
        }
    }
}
